/*
 * 
 *	Copyright (C) 2011 -- Alfred Reibenschuh
 *
 * 	This program is free software; you can redistribute it and/or 
 *	modify it under the terms of the GNU General Public License as 
 *	published by the Free Software Foundation; either version 2 of 
 *	the License, or (at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful, 
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *	GNU General Public License for more details.
 * 
 */

package redoak.php.ldap;

import java.util.logging.Logger;

import com.caucho.quercus.annotation.Optional;
import com.caucho.quercus.env.BooleanValue;
import com.caucho.quercus.env.Env;
import com.caucho.quercus.env.Value;
import com.caucho.util.L10N;

public class JndiLdapModule extends AbstractLdapModule 
{
	
	private static final Logger log = Logger.getLogger(JndiLdapModule.class.getName());
	private static final L10N L = new L10N(JndiLdapModule.class);

	
	//resource ldap_connect  ([ string $hostname = NULL  [, int $port = 389  ]] )
	public Value ldap_connect(Env env, @Optional String hosturi, @Optional Integer port)
	{
		ILdapSession ldap = new JndiLdapSession();
		if(port==0)
		{
			ldap.setPort(389);
		}
		else
		{
			ldap.setPort(port);
		}
		
		if(hosturi==null || hosturi.equals(""))
		{
			hosturi="127.0.0.1";
		}
		
		if(hosturi.startsWith("ldap://") || hosturi.startsWith("ldaps://"))
		{
			if(!ldap.setUri(hosturi))
			{
				return BooleanValue.FALSE;
			}
		}
		else
		{
			ldap.setHost(hosturi);
		}
		return env.wrapJava(ldap, true);
	}
}
